package io.cici.cc.mybatis.plus.core.injector.methods;

import io.cici.cc.mybatis.lite.mapping.MappedStatement;
import io.cici.cc.mybatis.lite.mapping.SqlSource;
import io.cici.cc.mybatis.plus.core.enums.SqlMethod;
import io.cici.cc.mybatis.plus.core.injector.Method;
import io.cici.cc.mybatis.plus.core.metadata.TableInfo;

/**
 * 根据 entity 条件删除记录
 */
public class Delete extends Method {

    public Delete() {
        this(SqlMethod.DELETE.getMethod());
    }

    /**
     * @param name 方法名
     * @since 3.5.0
     */
    public Delete(String name) {
        super(name);
    }

    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        String sql;
        SqlMethod sqlMethod = SqlMethod.LOGIC_DELETE;
        {
            sqlMethod = SqlMethod.DELETE;
            sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo),
                    sqlComment());
            SqlSource sqlSource = super.createSqlSource(configuration, sql, modelClass);
            return this.addDeleteMappedStatement(mapperClass, methodName, sqlSource);
        }
    }
}
